package com.zz.action;

import com.opensymphony.xwork2.ActionSupport;
import com.zz.concurrent.POI.Student;
import com.zz.service.StudentService;
import com.zz.utils.ExportUtils;
import net.sf.json.JSONArray;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Author: zhangzhen
 * DATE: Created in 2018/8/16 19:27
 * Description:
 */
public class StudentAction extends ActionSupport {

    private StudentService studentSerivce = new StudentService();
    private int page;
    private int rows;
    private String sort;
    private String order;

    private String className;
    private String methodName;

    private String fields;
    private String titles;

    public StudentService getStudentSerivce() {
        return studentSerivce;
    }

    public void setStudentSerivce(StudentService studentSerivce) {
        this.studentSerivce = studentSerivce;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getFields() {
        return fields;
    }

    public void setFields(String fields) {
        this.fields = fields;
    }

    public String getTitles() {
        return titles;
    }

    public void setTitles(String titles) {
        this.titles = titles;
    }

    // 数据列表
    public void list() {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
        List<Student> slist = getStudents();
        int total = studentSerivce.getTotal();
        String json = "{\"total\":" + total + "," + "\"rows\":" + JSONArray.fromObject(slist).toString() + "," +
                        "\"className\":\"" + StudentAction.class.getName() + "\"," +
                        "\"methodName\":\"getStudents\"}";

        try {
            response.getWriter().write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /* 获取学生信息*/
    private List<Student> getStudents() {
        List<Student> slist = studentSerivce.list(page, rows, sort, order);
        return slist;
    }
    /* 导出到前台列表为excel文件*/
    public void export() {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename = export.xls");
        // 创建Excel
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("sheet0");
        // 获取类
        try {
            Class clazz = Class.forName(className);
            Object o = clazz.newInstance();
            Method m = clazz.getDeclaredMethod(methodName);
            List list = (List) m.invoke(o);
            titles = new String(titles.getBytes("ISO-8859-1"), "UTF-8");
            ExportUtils.outputHeaders(titles.split(","), sheet);
            ExportUtils.outputColumns(fields.split(","),list,sheet,1);
            // 获取输出流,写入excel并关闭
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
